package br.com.werp.suporte.Enderecos;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import com.egen.util.jdbc.*;
import com.egen.util.text.*;

/**
 * Creation Date 02/12/2005 14:53:46
 * Last Modify Date 12/01/2007 08:58:15
 */

public class CepDetalheAction extends com.egen.util.struts.AbstractAction {

  public ActionForward perform_update_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();
      update_action(j, mapping, form, request, response);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void update_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    CepDetalheActionForm f = (CepDetalheActionForm) form;
    br.com.werp.suporte.dbobj.table.Cep table =
      new br.com.werp.suporte.dbobj.table.Cep();
    Vector sets = new Vector(10, 2);
    table.setCeps_logradouro(
      com.egen.util.text.FormatNumber.parseInt(f.getCeps_logradouro()));
    if (f.getCeps_logradouro() != null) {
      sets.addElement("ceps_logradouro");
    } else {
      sets.addElement("");
    };
    table.setCeps_bairro(
      com.egen.util.text.FormatNumber.parseInt(f.getCeps_bairro()));
    if (f.getCeps_bairro() != null) {
      sets.addElement("ceps_bairro");
    } else {
      sets.addElement("");
    };
    table.setCeps_cidade(
      com.egen.util.text.FormatNumber.parseInt(f.getCeps_cidade()));
    if (f.getCeps_cidade() != null) {
      sets.addElement("ceps_cidade");
    } else {
      sets.addElement("");
    };
    table.setCeps_numero(
      com.egen.util.text.FormatNumber.parseInt(f.getCeps_numero()));
    if (f.getCeps_numero() != null) {
      sets.addElement("ceps_numero");
    } else {
      sets.addElement("");
    };
    String[] set = new String[sets.size()];
    for (int i = 0; i < sets.size(); i++) {
      set[i] = (String) sets.elementAt(i);
    }
    int updated = j.update(table, set);
    if (updated == 0) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("warn.norowsupdated",": " + updated));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
    } else {
      Vector res_Cep = (Vector) session.getAttribute("res_Cep");
      if (res_Cep != null) {
        res_Cep.setElementAt(table, 0);
        br.com.werp.suporte.dbobj.table.Cep r =
          (br.com.werp.suporte.dbobj.table.Cep)
          res_Cep.elementAt(0);
        f.setCeps_logradouro(r.getCeps_logradouro() + "");
        f.setCeps_bairro(r.getCeps_bairro() + "");
        f.setCeps_cidade(r.getCeps_cidade() + "");
        f.setCeps_numero(r.getCeps_numero() + "");
      }
    }
    session.setAttribute(mapping.getName(), form);
  }

  public ActionForward perform_delete_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();
      delete_action(j, mapping, form, request, response);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void delete_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    CepDetalheActionForm f = (CepDetalheActionForm) form;
    br.com.werp.suporte.dbobj.table.Cep table =
      new br.com.werp.suporte.dbobj.table.Cep();
    table.setCeps_logradouro(
      com.egen.util.text.FormatNumber.parseInt(f.getCeps_logradouro()));
    table.setCeps_bairro(
      com.egen.util.text.FormatNumber.parseInt(f.getCeps_bairro()));
    table.setCeps_cidade(
      com.egen.util.text.FormatNumber.parseInt(f.getCeps_cidade()));
    table.setCeps_numero(
      com.egen.util.text.FormatNumber.parseInt(f.getCeps_numero()));
    j.delete(table);
    if (true) {
      Vector res_Cep = (Vector) session.getAttribute("res_Cep");
      if (res_Cep != null) {
        res_Cep.removeElementAt(0);
        br.com.werp.suporte.dbobj.table.Cep r =
          (br.com.werp.suporte.dbobj.table.Cep)
          res_Cep.elementAt(0);
        f.setCeps_logradouro(r.getCeps_logradouro() + "");
        f.setCeps_bairro(r.getCeps_bairro() + "");
        f.setCeps_cidade(r.getCeps_cidade() + "");
        f.setCeps_numero(r.getCeps_numero() + "");
      }
    } else {
      f.reset(mapping, request);
    }
    session.setAttribute(mapping.getName(), form);
  }

  public ActionForward perform_resetfull_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    try {
      session.removeAttribute("res_Cep");
      form.reset(mapping, request);
      session.setAttribute(mapping.getName(), form);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    return actionForward;
  }

}
